// 560 normal
/**
 * 超时
 * @param {number[]} nums
 * @param {number} k
 */
function subArrSum(nums, k) {
  let size = 0;
  for (let i = 0; i <= nums.length; i++) {
    for (let j = i + 1; j <= nums.length; j++) {
      if (sum(nums.slice(i, j)) === k) {
        size++;
      }
    }
  }
  return size;
}
function sum(nums) {
  let sum = 0;
  nums.forEach((item) => {
    sum += item;
  });
  console.log(sum);
  return sum;
}
console.log(subArrSum([1, 1, 1], 2));

// 动态获取sum
var subarraySum = function (nums, k) {
  let count = 0;
  for (let start = 0; start < nums.length; ++start) {
    let sum = 0;
    for (let end = start; end >= 0; --end) {
      sum += nums[end];
      if (sum == k) {
        count++;
      }
    }
  }
  return count;
};
